package _021_10_8

//超出内存
func twoSum2(nums []int, target int) []int {
	ret := make([][]int, 0)
	for i := 0; i < len(nums); i++ {
		for j := i + 1; j < len(nums); j++ {
			item := make([]int, 0)
			item = append(item, i)
			item = append(item, j)
			ret = append(ret, item)
		}
	}

	for i := 0; i < len(ret); i++ {
		if ret[i][0]+ret[i][1] == target {
			return ret[i]
		}
	}
	return nil
}

//超出时间限制
func twoSum3(nums []int, target int) []int {
	for i := 0; i < len(nums); i++ {
		for j := i + 1; j < len(nums); j++ {
			if nums[i]+nums[j] == target {
				return []int{nums[i], nums[j]}
			}
		}
	}

	return nil
}

func twoSum(nums []int, target int) []int {
	l, r := 0, len(nums)-1

	for l < r {
		ret := nums[l] + nums[r]
		if ret < target {
			l++
		} else if ret > target {
			r--
		} else {
			return []int{nums[l], nums[r]}
		}
	}

	return nil
}
